home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / unix.subproj / run.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  3.0 KB  |  114 lines

  1. /* run.c
  2.    Run a program.
  3.  
  4.    Copyright (C) 1992, 1993, 1994 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #include "uudefs.h"
  29. #include "sysdep.h"
  30. #include "system.h"
  31.  
  32. #include <errno.h>
  33.  
  34. /* Start up a new program.  */
  35.  
  36. boolean
  37. fsysdep_run (ffork, zprogram, zarg1, zarg2)
  38.      boolean ffork;
  39.      const char *zprogram;
  40.      const char *zarg1;
  41.      const char *zarg2;
  42. {
  43.   char *zlib;
  44.   const char *azargs[4];
  45.   int aidescs[3];
  46.   pid_t ipid;
  47.  
  48.   /* If we are supposed to fork, fork and then spawn so that we don't
  49.      have to worry about zombie processes.  */
  50.   if (ffork)
  51.     {
  52.       ipid = ixsfork ();
  53.       if (ipid < 0)
  54.     {
  55.       ulog (LOG_ERROR, "fork: %s", strerror (errno));
  56.       return FALSE;
  57.     }
  58.  
  59.       if (ipid != 0)
  60.     {
  61.       /* This is the parent.  Wait for the child we just forked to
  62.          exit (below) and return.  */
  63.       (void) ixswait ((unsigned long) ipid, (const char *) NULL);
  64.  
  65.       /* Force the log files to be reopened in case the child just
  66.          output any error messages and stdio doesn't handle
  67.          appending correctly.  */
  68.       ulog_close ();
  69.  
  70.       return TRUE;
  71.     }
  72.  
  73.       /* This is the child.  Detach from the terminal to avoid any
  74.      unexpected SIGHUP signals.  At this point we are definitely
  75.      not a process group leader, so usysdep_detach will not fork
  76.      again.  */
  77.       usysdep_detach ();
  78.  
  79.       /* Now spawn the program and then exit.  */
  80.     }
  81.  
  82.   zlib = zbufalc (sizeof SBINDIR + sizeof "/" + strlen (zprogram));
  83.   sprintf (zlib, "%s/%s", SBINDIR, zprogram);
  84.  
  85.   azargs[0] = zlib;
  86.   azargs[1] = zarg1;
  87.   azargs[2] = zarg2;
  88.   azargs[3] = NULL;
  89.  
  90.   aidescs[0] = SPAWN_NULL;
  91.   aidescs[1] = SPAWN_NULL;
  92.   aidescs[2] = SPAWN_NULL;
  93.  
  94.   /* We pass fsetuid and fshell as TRUE, which permits uucico and
  95.      uuxqt to be replaced by (non-setuid) shell scripts.  */
  96.   ipid = ixsspawn (azargs, aidescs, TRUE, FALSE, (const char *) NULL,
  97.            FALSE, TRUE, (const char *) NULL,
  98.            (const char *) NULL, (const char *) NULL);
  99.   ubuffree (zlib);
  100.  
  101.   if (ipid < 0)
  102.     {
  103.       ulog (LOG_ERROR, "ixsspawn: %s", strerror (errno));
  104.       if (ffork)
  105.     _exit (EXIT_FAILURE);
  106.       return FALSE;
  107.     }
  108.  
  109.   if (ffork)
  110.     _exit (EXIT_SUCCESS);
  111.  
  112.   return TRUE;
  113. }
  114.